/**
 * 
 */
package com.qdairlines.controller.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qdairlines.common.ControllerUtil;
import com.qdairlines.common.Page;
import com.qdairlines.constant.BaseConsts;
import com.qdairlines.constant.DictionaryConsts;
import com.qdairlines.constant.RequestConsts;
import com.qdairlines.controller.base.BaseController;
import com.qdairlines.entity.user.Role;
import com.qdairlines.entity.user.RolePermission;
import com.qdairlines.entity.user.RolePermissionDto;
import com.qdairlines.entity.user.User;
import com.qdairlines.service.dict.DictionaryService;
import com.qdairlines.service.user.PermissionService;
import com.qdairlines.service.user.RolePermissionService;
import com.qdairlines.service.user.RoleService;
import com.qdairlines.service.user.UserService;

/**
 * @author pangyannan
 * @date 2017年3月7日
 * @company QingDao Airlines
 * @description 角色控制
 */
@Controller
@RequestMapping("role")
public class RoleController  extends BaseController{
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermissionService permissionService;
	@Autowired 
	private RolePermissionService rolePermissionService;
	@Autowired
	private DictionaryService dictionaryService;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 进入页面
	 * @return
	 */
    @RequestMapping( value = "toPage" ,produces =RequestConsts.PRODUCES)
    public String toPage(HttpServletRequest request,Model model) {
    	model.addAttribute(BaseConsts.LANGUAGE, ControllerUtil.getLanguage(request));
    	return "/user/rolelist";
    }	

    
    /**
     * 获取用户数据
     * @param request
     * @param workcode
     * @param model
     * @return
     */
    @RequestMapping(value = "getRole" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String getRole(HttpServletRequest request,Integer roleId) {
    	Role  role = roleService.getByPrimaryKey(roleId);
     	return ControllerUtil.getJson(role);
    }
    
    /**
     * 用户列表
     * @param request
     * @param roleDataItem
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(value = "listRolePage",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String getDateList(HttpServletRequest request,Role role) {
    	Page page = roleService.getPageFromRequest(request);
    	page = roleService.listData(role, page);
    	List<Role> list = page.getRows();
    	dictionaryService.convertDictValueToText(list, "status", "statusName", DictionaryConsts.ROLE_STATUS, ControllerUtil.getLanguage(request));
    	return ControllerUtil.getJson(page);
    }
    
    /**
     * 保存角色
     * @param request
     * @param dutySchedule
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "save",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String save(HttpServletRequest request,Role role) { 
    	Role roleCk1 = roleService.getRoleByRoleCode(role.getRoleCode());
    	Role roleCk2 = roleService.getRoleByRoleName(role.getRoleName());
    	
    	if(role.getId() == null){
    		//新增判断
    		if(roleCk1 != null || roleCk2 != null){
    			return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    		}
    		//新增
    		role.setCreateBy(ControllerUtil.getUser(request).getWorkcode());
    		role.setCreateTime(new Date());
    		role.setStatus(BaseConsts.POJO_STATUS_ABLE);
    		roleService.saveSelective(role);	
    	}else{
    		//修改判断
    		if(roleCk1 != null && !role.getId().equals(roleCk1.getId())){
    			return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    		}
    		if(roleCk2 != null && !role.getId().equals(roleCk2.getId())){
    			return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    		}
    		
    		//修改
    		role.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    		role.setUpdateTime(new Date());
    		roleService.updateByPrimaryKeySelective(role);
    	}
    	
    	return  JSONObject.fromObject(role).toString();
    }
    
    /**
     * 禁用角色
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "disable" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String disable(Integer id,HttpServletRequest request){
    	List<User>  userList = userService.listUserByRoleId(id);
    	if(userList != null && userList.size() >0){
    		return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    	}
    	Role role = new Role();
    	role.setId(id);
    	role.setStatus(BaseConsts.POJO_STATUS_DISABLE);
    	role.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    	role.setUpdateTime(new Date());
		roleService.updateByPrimaryKeySelective(role);

		return JSONObject.fromObject(BaseConsts.RESPONSE_MAP_SUCCESS).toString();
    }
    
    /**
     * 启用角色
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "able" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String able(Integer id,HttpServletRequest request){
    	Role role = new Role();
    	role.setId(id);
    	role.setStatus(BaseConsts.POJO_STATUS_ABLE);
    	role.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    	role.setUpdateTime(new Date());
		roleService.updateByPrimaryKeySelective(role);
    	return JSONObject.fromObject(BaseConsts.RESPONSE_MAP_SUCCESS).toString();
    }
    
    
    
    
    /**
     * 查询所有权限，并且将与该roleId有关联的角色标识出来
     * @param workcode
     * @param request
     * @return
     */
    @RequestMapping(value = "listRolePermission" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String listRolePermission(Integer roleId,String isRelated){
    	if(roleId == null){
    		return null;
    	}else{
	    	List<RolePermissionDto> list = rolePermissionService.listRolePermissionDtoByRoleId(roleId,isRelated );
	    	return ControllerUtil.getJson(list);
    	}
    }
    
    @RequestMapping(value = "getRolePermissionCount" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String getRolePermissionCount(Integer roleId,String isRelated){
    	if(roleId == null){
    		return null;
    	}else{
	    	List<RolePermissionDto> list = rolePermissionService.listRolePermissionDtoByRoleId(roleId,isRelated );
	    	return ControllerUtil.getJson(list==null?0:list.size());
    	}
    }
    
    
    /**
     * 删除角色权限
     * @param userRoleid
     * @param request
     * @return
     */
    @RequestMapping(value = "deleteRolePermission" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String deleteRolePermission(Integer rolePermissionId,HttpServletRequest request){
    	if(rolePermissionId != null){
	    	rolePermissionService.deleteByPrimaryKey(rolePermissionId);
	    	return JSONObject.fromObject(BaseConsts.RESPONSE_MAP_SUCCESS).toString();
    	}
    	return JSONObject.fromObject(BaseConsts.RESPONSE_MAP_ERROR).toString();
    }
    
    /**
     * 添加用户角色
     * @param roleId
     * @param workcode
     * @param request
     * @return
     */
    @RequestMapping(value = "saveRolePermission" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String saveRolePermission(Integer roleId, Integer permissionId,HttpServletRequest request){
    	RolePermission rolePermission = new RolePermission();
    	rolePermission.setRoleId(roleId);
    	rolePermission.setPermissionId(permissionId);
    	
    	rolePermission.setCreateBy(ControllerUtil.getUser(request).getWorkcode());
    	rolePermission.setCreateTime(new Date());

    	rolePermissionService.saveSelective(rolePermission);
    	return JSONObject.fromObject(BaseConsts.RESPONSE_MAP_SUCCESS).toString();
    }
    
    @RequestMapping(value = "delete" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String delete(@RequestParam Integer roleId){
    	roleService.deleteByPrimaryKey(roleId);
		return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }
    
    @RequestMapping(value="toAddUserPage",produces = RequestConsts.PRODUCES)
    public String toAddUserPage(@RequestParam Integer roleId,Model model,HttpServletRequest request){
    	model.addAttribute(BaseConsts.LANGUAGE,ControllerUtil.getLanguage(request));
    	
    	Role role =roleService.getByPrimaryKey(roleId);
    	model.addAttribute("role", role);
    	return "/user/roleAddUser";
    }
    
    @RequestMapping(value="listUserByRoleId",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String listUserByRoleId(@RequestParam Integer roleId){
    	List<User> list = userService.listUserByRoleId(roleId);
    	return ControllerUtil.getJson(list);
    }
    
    @RequestMapping(value="listUserByRoleIdNotRelate",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String listUserByRoleIdNotRelate(@RequestParam Integer roleId){
    	List<User> list = userService.listUserByRoleIdNotRelate(roleId);
    	return ControllerUtil.getJson(list);
    }
    
}
